package fr.univ_tln.inf9.exaplanning.gui.salle;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import javax.swing.JList;


import fr.univ_tln.inf9.exaplanning.api.salle.Material;
import fr.univ_tln.inf9.exaplanning.api.salle.event.ListMaterialChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.salle.event.MaterialAddedToListEvent;
import fr.univ_tln.inf9.exaplanning.api.salle.event.MaterialRemovedToListEvent;
import fr.univ_tln.inf9.exaplanning.controleur.salle.RoomController;
import fr.univ_tln.inf9.exaplanning.controleur.salle.RoomView;

/**
 * 
 * @author pivi
 *
 */


public class JFrameListMaterial extends RoomView implements ActionListener {

	private JFrame frame = null;
	private JPanel contentPane = null;
	private JList listMaterial = null;
	private JScrollPane scrollMaterial = null;
	private DefaultListModel jListModel = null; 
	private static List<String> listzero = null;
	private String nameRoom = null;
	
	private JButton addButton;
	private JButton removeButton;
	
	private JComboBox material;
	private static String[] myMaterials = {"projo", "tableau","chips","brosse à dent","scanner 3d","tablette graphique",
										"nutella","ordinateur 32bits","ordinateur 64bits","oridnateur quantique"};
	
	
	public JFrameListMaterial(RoomController controller) {
		this(controller, listzero,null);
	}
	
	public JFrameListMaterial(RoomController controller, List<String> m,String nameR){
		super(controller); 
		this.nameRoom = nameR;
		buildFrame(m);
	}
	
	private void buildFrame(List<String> m) {
		frame = new JFrame();
		
		contentPane = new JPanel();
		this.contentPane.setSize(20, 30);
		
		
		jListModel = new DefaultListModel();
		
		/**
		 * Insertion des éléments
		 */
		
		Iterator<String> it = m.iterator(); 
		while(it.hasNext()) {
			jListModel.addElement(it.next());
		}
		
		listMaterial = new JList(jListModel);
		
		scrollMaterial = new JScrollPane(listMaterial);
		contentPane.add(scrollMaterial);
		material   = new JComboBox(myMaterials);
		contentPane.add(material);
		this.addButton = new JButton("ajouter matériel");
		this.addButton.addActionListener(this);
		
		this.removeButton = new JButton("enlever matériel");
		this.removeButton.addActionListener(this);
		
		contentPane.add(addButton);
		contentPane.add(removeButton);
		
		frame.setContentPane(contentPane);
		frame.setTitle("Liste des matériels de la salle "+nameRoom);
		frame.pack();
	}
	
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getActionCommand().equals("ajouter matériel")){
			Material m = new Material((String)this.material.getSelectedItem());
			this.getController().notifyElementAddedToList(m);
		}
		if(arg0.getActionCommand().equals("enlever matériel")){
		this.getController().notifyElementRemovedToList((String)this.listMaterial.getSelectedValue());
		}
	}
	
	
	@Override
	public void ListMaterialChanged(ListMaterialChangedEvent event) {
		jListModel.addElement(event.getNewListMaterial());
		
	}

	@Override
	public void MaterialAddedToList(MaterialAddedToListEvent event) {
		jListModel.addElement(event.getNewEquipment());
		
	}

	@Override
	public void MaterialRemovedToList(MaterialRemovedToListEvent event) {
		jListModel.removeElement(event.getEquipmentToRemove());

	}

	@Override
	public void close() {
		frame.dispose();
	}

	@Override
	public void display() {
		frame.setVisible(true);
	}

	



} 
